package designpattern.observer.mq;


import java.util.ArrayList;
import java.util.List;

public class OrderSubject<E> extends Subject<E> {
    private List<Observer<E>> observers = new ArrayList<>();
    @Override
    Result subscribe(Observer<E> observer) {
        if (this.observers.contains(observer))
            return Result.builder().code(200).msg("已经订阅过").build();
        this.observers.add(observer);
        return Result.builder().code(200).msg("订阅成功").build();
    }

    @Override
    Result unsubscribe(Observer<E> orderObserver) {
        this.observers.remove(orderObserver);
        return Result.builder().code(200).msg("已取消订阅").build();
    }

    @Override
    void notify(E event) {
        observers.forEach(observer -> {
            observer.handEvent(event);
        });
    }
}
